/**
 * 用户服务
 * @TransactionAnnotation表示开启事务
 */
import TransactionAnnotation from '../../package/TransactionAnnotation'

export default class UserBindingRoleService {
    /**
     * 注册用户
     * @param table                 指定表名称
     * @param user                  用户属性
     * @returns {Promise<*>|void}
     *
     */

    addUserRoleEntity(entity) {
        return this['session'].save('t_user_role', entity);
    }

    /**
     * 批量绑定
     * @param entitys
     * @returns {Promise<unknown>}
     */
    @TransactionAnnotation
    addBatchUserRoleEntity(entitys) {
        return new Promise(async (resolve, reject) => {
            try {
                if (!Array.isArray(entitys)) {
                    throw ({message: '批量录入的Entity不是数组!'})
                }
                const array = [];
                for (let i = 0; i < entitys.length; i++) {
                    const obj = await this.addUserRoleEntity(entitys[i]);
                    array.push(obj);
                }
                resolve(array);
            } catch (e) {
                reject(e);
            }
        })
    }

    /**
     * 保存
     * @param table
     * @param userName
     * @returns {*}
     */
    removeUserRoleEntity(entity) {
        const sql = "delete from t_user_role where t_rid = ? and t_pid = ?";
        return this['session'].executeSQL(sql,[entity.t_rid,entity.t_pid]);
    }

    /**
     * 批量删除
     * @param entitys
     * @returns {Promise<unknown>}
     */
    @TransactionAnnotation
    removeBatchUserRoleEntity(entitys) {
        return new Promise(async (resolve, reject) => {
            try {
                if (!Array.isArray(entitys)) {
                    throw ({message: '批量录入的Entity不是数组!'})
                }
                const array = [];
                for (let i = 0; i < entitys.length; i++) {
                    const obj = await this.removeUserRoleEntity( entitys[i]);
                    array.push(obj);
                }
                resolve(array);
            } catch (e) {
                reject(e);
            }

        })
    }


    /**
     * 查询
     * @param table
     * @param start
     * @param size
     * @returns {*}
     */
    listUserRoleEntities(uid) {
        return this['session'].executeSQL("SELECT t_role.`t_id`,t_role.`t_roleName`, a.`t_uid` FROM t_role LEFT JOIN (SELECT * FROM t_user_role WHERE t_uid = ?) AS a ON t_role.t_id = a.`t_rid`",  [uid] );
    }


}


